﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CallCenter.BLL;
using FTP操作;
using Maticsoft.Model;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace CallCenter
{
    public partial class txtPinyin : Form
    {
        public txtPinyin()
        {
            InitializeComponent();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string strPath = openFileDialog1.FileName;
                using (FileStream stream = File.OpenRead(strPath))
                {
                    IWorkbook workbook = new HSSFWorkbook(stream);
                    ISheet sheet = workbook.GetSheetAt(0);
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        //MessageBox.Show(sheet.LastRowNum.ToString());
                        CustomersModel cust = new CustomersModel();
                        ICell cell门店编号 = sheet.GetRow(i).GetCell(0);
                        cust.DeptNo = cell门店编号.StringCellValue;
                        ICell cell客户姓名 = sheet.GetRow(i).GetCell(1);
                        cust.Name = cell客户姓名.StringCellValue;
                        ICell cell固定电话 = sheet.GetRow(i).GetCell(2);
                        if (cell固定电话 == null)
                        {
                            cust.TelPhone = "";
                        }
                        else
                        {
                            cust.TelPhone = cell固定电话.StringCellValue;
                        }
                        ICell cell手机 = sheet.GetRow(i).GetCell(3);
                        if (cell手机.CellType == CellType.STRING)
                        {
                            cust.MobilePhone = cell手机.StringCellValue;
                        }
                        else
                        {
                            cust.MobilePhone = cell手机.NumericCellValue.ToString();
                        }
                        ICell cell车牌号 = sheet.GetRow(i).GetCell(4);
                        if (!new CustomerBLL().Check(cell车牌号.StringCellValue))
                        {
                            continue;
                        }
                        else
                        {
                            cust.CarNum = cell车牌号.StringCellValue;
                        }
                        ICell cell车架号 = sheet.GetRow(i).GetCell(5);
                        cust.BracketNum = cell车架号.StringCellValue;
                        ICell cell购买日期 = sheet.GetRow(i).GetCell(6);
                        cust.BuyDate = cell购买日期.DateCellValue;
                        ICell cell品牌 = sheet.GetRow(i).GetCell(7);
                        cust.Brand = cell品牌.StringCellValue;
                        ICell cell型号 = sheet.GetRow(i).GetCell(8);
                        cust.TypeNum = cell型号.StringCellValue;
                        cust.NamePinyin = "";
                        cust.PostCode = "";
                        cust.Remarks = "";
                        cust.Suggestion = "";
                        cust.Address = "";
                        cust.Email = "";
                        //MessageBox.Show("OK");
                        new CustomerBLL().AddNew(cust);
                    }
                    MessageBox.Show("导入成功");
                    ReLoad();
                }
            }
        }

        private void FormCustMgr_Load(object sender, EventArgs e)
        {
            ReLoad();
        }
        string strDate = DateTime.Now.ToString("yyyyMMdd");

        /// <summary>
        /// 下载文件，并验证完整性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDownload_Click(object sender, EventArgs e)
        {
            FTPClient fc = new FTPClient("127.0.0.1", "Test", "m-6685265");
            fc.Download("02" + strDate + ".xls", @"D:\Demo\CallCenter\CallCenter\CallCenter\Data\02" + strDate + ".xls");
            String MD5 = ServerMD5Helper.MD5Value(@"D:\Demo\CallCenter\CallCenter\CallCenter\Data\02" + strDate + ".xls");
               // MessageBox.Show(MD5);
                string newMD5 = ServerMD5Helper.GetMD5(MD5 + "Test");
                fc.Download("02" + strDate + ".txt", @"D:\Demo\CallCenter\CallCenter\CallCenter\Data\02" + strDate + ".txt");
                string serverMD5 = File.ReadAllText(@"D:\Demo\CallCenter\CallCenter\CallCenter\Data\02" + strDate + ".txt");

                //MessageBox.Show(newMD5 + "            " + serverMD5);
                if (newMD5 == serverMD5)
                {
                    MessageBox.Show("下载验证完成");
                }
                else
                {
                    MessageBox.Show("被窜改！小心");
                }
        }

        private void btnCreatePinYin_Click(object sender, EventArgs e)
        {
            new CustomerBLL().CreatePinyin();
            MessageBox.Show("OK");
            ReLoad();
        }

        void ReLoad()
        {
            CustomersModel[] customers = new CustomerBLL().ListAll();
            dgvCustomer.DataSource = customers;
         }

        //todo:同个文本款加入中文名字搜索功能   select * from T_Customers where name=@txtStr or namePinyin = @txtStr
        private void btnPinyinSearch_Click(object sender, EventArgs e)
        {
            string pinyin = txtStrPinyin.Text;
            CustomersModel[] table = new CustomerBLL().GetByPinyin(pinyin);
            dgvCustomer.DataSource = table;
        }
    }
}
